package com.tamecode.flowable.spring.gateway;

import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.engine.test.Deployment;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskQuery;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.HashMap;
import java.util.List;

import static junit.framework.TestCase.assertEquals;

/**
 * @Author: Liqc
 * @Date: 2021/11/16 14:57
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:SpringTransactionIntegrationTest-context.xml")
public class IncludeGateway {

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;

    @Autowired
    RepositoryService repositoryService;

    @Test
    @Deployment // 自动加载[类名.方法.bpmn20.xml]的模板文件
    public void includeGateway() {

        String deploymentId = repositoryService
                .createDeployment()
                .addClasspathResource("processes/forkJoin.bpmn20.xml")
                .deploy()
                .getId();
        System.out.println(deploymentId);

        HashMap<String, Object> variableMap = new HashMap<String, Object>();
        variableMap.put("paymentReceived", true);
        variableMap.put("shipOrder", true);

        ProcessInstance pi = runtimeService.startProcessInstanceByKey("forkJoin", variableMap);

        TaskQuery query = taskService.createTaskQuery()
                .processInstanceId(pi.getId())
                .orderByTaskName()
                .asc();

        List<Task> tasks = query.list();
        assertEquals(1, tasks.size());

        Task task = tasks.get(0);
        assertEquals("Ship Order", task.getName());
    }

}
